package Handling;

import java.io.IOException;
import java.sql.*;
import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class WuerfelnServlet
 */
@WebServlet(name = "ServletDice", urlPatterns = {"/ServletDice"})
public class ServletDice extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public GameAdmin ga = new GameAdmin();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDice() {
        super();
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String player =(String) session.getAttribute("PlayerID") ;
    	String game = (String) session.getAttribute("GameID");
    	int playerid = Integer.parseInt(player);
    	int gameid = Integer.parseInt(game);
    	
		int dice1 =ga.throwDice();
		int dice2 =ga.throwDice();
		int position;
		try {
			position = ga.newField(gameid, playerid, dice1, dice2);
			try {
				out.println( "{" + "\"dice1\":"+dice1+"," + "\"dice2\":"+dice2+"," + "\"position\":"+position+"" + "}" );
			 } finally { out.close(); } 
				// TODO Auto-generated method stub
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
